/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.se2.meteocal.gui;

import it.polimi.se2.meteocal.control.UserManager;
import it.polimi.se2.meteocal.entity.Users;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

/**
 *
 * @author Ugo
 */
@Named(value = "signupBean")
@RequestScoped
public class SignupBean {

    @EJB
    private UserManager um;

    private Users user;

    public SignupBean() {
    }

    public Users getUser() {
        if (user == null) {
            user = new Users();
        }
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
    }

    public String register() {
        try{
            um.save(user);
        }catch(EJBException ejbe){
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"","Registration failed. This email is already used."));
            return "signup";
        }
        return "login?faces-redirect=true";
    }
    
   
}
